import router from './router'
import { getToken, setToken } from '@/utils/auth'
import getPageTitle from '@/utils/get-page-title'
import store from './store'

const whiteList = ['/login', '/share', '/password', '/merchantticket', '/agreement', '/mean']
router.beforeEach(async(to, from, next) => {
  if (store.state.userInfo.store_id === undefined && whiteList.indexOf(to.path) === -1) {
    await store.dispatch('getUserInfo')
    console.log('await之后 往下执行了')
  }
  document.title = getPageTitle(to.meta.title)
  const hasToken = getToken()
  if (hasToken) {
    if (to.path.indexOf('login') !== -1) {
      next({ path: '/person' })
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?storeId=${store.state.userInfo.store_id}`)
    }
  }
})
